package com.huayi.gg;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;

import com.danikula.videocache.HttpProxyCacheServer;
import com.github.gzuliyujiang.oaid.DeviceIdentifier;
import com.huayi.gg.config.API;
import com.huayi.gg.model.ServerBean;
import com.huayi.gg.service.OKHttpUpdateHttpService;
import com.huayi.gg.socket.MyServer;
import com.huayi.gg.socket.ServerInterface;
import com.huayi.gg.ui.MyFileNameGenerator;
import com.xuexiang.xupdate.XUpdate;
import com.xuexiang.xupdate.entity.UpdateError;
import com.xuexiang.xupdate.listener.OnUpdateFailureListener;
import com.xuexiang.xupdate.utils.UpdateUtils;

import java.net.URISyntaxException;

import io.socket.client.IO;


public class MyApplication extends Application {
    protected MyServer server;
    private static String LOG_TAG="Application";
    protected io.socket.client.Socket mSocket;
    private Context mcontent;
    private SharedPreferences sp;
    public io.socket.client.Socket getSocket() {
        return mSocket;
    }

    @Override
    public void onCreate() {
        super.onCreate();


        DeviceIdentifier.register(this);


        sp = getSharedPreferences("cache", Context.MODE_PRIVATE);

        mcontent= this;

        initSocket();

        XUpdate.get()
                .isWifiOnly(false)     //默认设置只在wifi下检查版本更新
                .isGet(true)          //默认设置使用get请求检查版本
                .isAutoMode(true)    //默认设置非自动模式，可根据具体使用配置
                .param("VersionCode", UpdateUtils.getVersionCode(this)) //设置默认公共请求参数
                .param("AppKey", getPackageName())
//                .debug(true)
                .setOnUpdateFailureListener(new OnUpdateFailureListener() { //设置版本更新出错的监听
                    @Override
                    public void onFailure(UpdateError error) {

                    }
                })
                .setIUpdateHttpService(new OKHttpUpdateHttpService()) //这个必须设置！实现网络请求功能。
                .init(this);




    }



    private HttpProxyCacheServer proxy;
    public static HttpProxyCacheServer getVideoCacheProxy(Context context) {
        MyApplication app = (MyApplication) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer(this);

    }

    public MyServer getserver(){

        return server;
    }

    public void  stopSocket(){
        server.close();
    }


    private void initSocket() {
        try {
            if (API.Socket_URL != "") {
                IO.Options option = new IO.Options();
                option.forceNew = true;
                option.reconnection = true;
                option.reconnectionDelay = 2000;
                mSocket = IO.socket(API.Socket_URL, option);
            }
            String code =API.getDeviceId(this);
            server = new MyServer(new ChatListenUIRefresh(), mcontent);
            server.init(code);
            server.heartbeat(mHandler);



        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {

                    }catch (Exception e){

                    }

                    break;

            }
        }
    };

    //socket客户端事件监听处理
    private class ChatListenUIRefresh implements ServerInterface {


        @Override
        public void onMessageListen(ServerBean chatBean) {

            String code = sp.getString("code", "");
            if (chatBean.code.equals(code)) {
                Intent intent = new Intent();
                intent.setAction(chatBean.type);
                intent.putExtra("chatbean",chatBean);
                sendBroadcast(intent);
            }else if(chatBean.codelist.indexOf(code)!=-1){
                Intent intent = new Intent();
                intent.setAction(chatBean.type);
                intent.putExtra("chatbean",chatBean);
                sendBroadcast(intent);
            }


        }

        @Override
        public void onConnect(boolean res) {
            if (res) {

            }
        }

        @Override
        public void onError() {

        }

        @Override
        public void onDisconnect() {

        }

    }

}
